package com.fstec.ks.master.dao.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.fstec.ks.master.dao.ServerRoundDao;
import com.fstec.ks.master.dao.cond.ServerRoundCond;
import com.fstec.ks.master.dao.jdbc.extractor.ServerRoundExtractor;
import com.fstec.ks.master.dao.jdbc.setter.ServerRoundSetter;
import com.fstec.ks.master.vo.ServerRoundVO;
import com.sinotn.id.SeqUUIDGenerator;
import com.sinotn.spring.jdbc.PageQuery;
import com.sinotn.spring.jdbc.ParameterSetter;
import com.sinotn.spring.jdbc.SqlDialect;

public class DefaultServerRoundDao extends JdbcDaoSupport implements ServerRoundDao {
	@Autowired
	private SqlDialect sqlDialect;
	private String schema;
	private String tableName="km_server_round";

	public void setSchema(String schema) {
		this.schema = schema;
	}
	@Override
	public int insert(ServerRoundVO serverRoundVO) {
		StringBuilder sb=new StringBuilder();
		sb.append("INSERT INTO ").append(this.schema).append(".").append(this.tableName);
		sb.append(" (").append(ServerRoundExtractor.getColumns());
		sb.append(") VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
		if(serverRoundVO.getId()==null){
			serverRoundVO.setId(SeqUUIDGenerator.genSequenceUUID(serverRoundVO.getRoundBeginTime().getTime()));
		}
		ParameterSetter ps=new ParameterSetter(12);
		//id,login_count,submit_count,room_count,room_sign_count,examinee_count,
		ps.set(serverRoundVO.getId());
		ps.set(serverRoundVO.getLoginCount());
		ps.set(serverRoundVO.getSubmitCount());
		ps.set(serverRoundVO.getRoomCount());
		ps.set(serverRoundVO.getRoomSignCount());
		ps.set(serverRoundVO.getExamineeCount());
		//round_end_time,paper_down,exam_server_id,round_begin_time,upload_time,backup_time
		ps.set(serverRoundVO.getRoundEndTime().getTime());
		ps.set(serverRoundVO.getPaperDown());
		ps.set(serverRoundVO.getExamServerId());
		ps.set(serverRoundVO.getRoundBeginTime().getTime());
		ps.set(serverRoundVO.getUploadTime()==null?0L:serverRoundVO.getUploadTime().getTime());
		ps.set(serverRoundVO.getBackupTime()==null?0L:serverRoundVO.getBackupTime().getTime());
		
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}

	@Override
	public int update(ServerRoundVO serverRoundVO) {
		StringBuilder sb=new StringBuilder();
		sb.append("UPDATE ").append(this.schema).append(".").append(this.tableName);
		sb.append(" SET login_count=?,submit_count=?,room_sign_count=?,paper_down=?,upload_time=?,backup_time=?");
		sb.append(" WHERE id=?");
		ParameterSetter ps=new ParameterSetter(7);
		
		ps.set(serverRoundVO.getLoginCount());
		ps.set(serverRoundVO.getSubmitCount());
		ps.set(serverRoundVO.getRoomSignCount());
		ps.set(serverRoundVO.getPaperDown());
		ps.set(serverRoundVO.getUploadTime()==null?0L:serverRoundVO.getUploadTime().getTime());
		ps.set(serverRoundVO.getBackupTime()==null?0L:serverRoundVO.getBackupTime().getTime());
		ps.set(serverRoundVO.getId());
		
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}

	@Override
	public ServerRoundVO select(String id) {
		StringBuilder sb=new StringBuilder();
		sb.append("SELECT ").append(ServerRoundExtractor.getColumns());
		sb.append(" FROM ").append(this.schema).append(".").append(this.tableName);
		sb.append(" WHERE id=?");
		
		ParameterSetter ps=new ParameterSetter(1);
		ps.set(id);
		List<ServerRoundVO> list=this.getJdbcTemplate().query(sb.toString(), ps, new ServerRoundExtractor());
		if(list.isEmpty()) return null;
		return list.get(0);
	}

	@Override
	public List<ServerRoundVO> selects(int pageIdx, int pageSize, ServerRoundCond cond) {
		StringBuilder sb=new StringBuilder();
		sb.append("SELECT ").append(ServerRoundExtractor.getColumns());
		sb.append(" FROM ").append(this.schema).append(".").append(this.tableName);
		
		ServerRoundSetter setter=new ServerRoundSetter(cond);
		sb.append(setter.getWhere());
		JdbcTemplate jdbcTemplate=this.getJdbcTemplate();
		if(pageSize==0){
			return jdbcTemplate.query(sb.toString(), setter, new ServerRoundExtractor());
		}
		PageQuery pageQuery=this.sqlDialect.getPageQuery(pageIdx, pageSize, sb.toString());
		setter.setPageQuery(pageQuery);
		return jdbcTemplate.query(pageQuery.getQuerySql(), setter, new ServerRoundExtractor());
	}
	@Override
	public int deleteServerRound(String serverId) {
		StringBuilder sb=new StringBuilder();
		sb.append("DELETE FROM ").append(this.schema).append(".").append(this.tableName);
		sb.append(" WHERE exam_server_id=?");
		
		ParameterSetter ps=new ParameterSetter(1);
		ps.set(serverId);
		return this.getJdbcTemplate().update(sb.toString(), ps);
	}
}
